package com.acgist.polaris.compressor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import com.acgist.polaris.Compressor;
import com.acgist.polaris.entity.FileEntity;
import com.acgist.polaris.entity.PayloadEntity;
import com.acgist.polaris.entity.StreamEntity;

/**
 * <p>文件压缩机</p>
 * 
 * @author acgist
 */
public class FileCompressor implements Compressor<File, File> {
	
	private final FileEntity fileEntity;
	
	public FileCompressor() {
		this.fileEntity = FileEntity.newFileEntity();
	}

	@Override
	public void gc(File input, File output) throws IOException {
		// TODO：递归压缩
		this.fileEntity.setSize(input.length());
		this.fileEntity.setDate(input.lastModified());
		// TODO：修改相对路径
		this.fileEntity.setPath(input.getPath());
		final OutputStream outputStream = new FileOutputStream(output);
		outputStream.write(this.fileEntity.buildDescription());
		final PayloadEntity payloadEntity = new PayloadEntity(new FileInputStream(input), outputStream);
		payloadEntity.gc();
	}

	@Override
	public void ungc(File input, File output) throws IOException {
		// TODO：递归解压
		final InputStream inputStream = new FileInputStream(input);
		this.fileEntity.buildDescription(inputStream);
	}

	@Override
	public List<StreamEntity> list(File input) {
		return null;
	}
	
}
